/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BottomGaugePanel.java
 *
 * Created on 23.05.2011, 14:53:56
 */
package com.codeminders.controltower;

import com.codeminders.ardrone.ARDrone;
import com.codeminders.ardrone.NavData;
import com.codeminders.ardrone.NavDataListener;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
 *
 * @author normenhansen
 */
@SuppressWarnings("serial")
public class BottomGaugePanel extends javax.swing.JPanel implements NavDataListener {

    private BufferedImage commImage;
    private AtomicReference<ARDrone> drone = new AtomicReference<ARDrone>();

    /** Creates new form BottomGaugePanel */
    public BottomGaugePanel() {
        initComponents();
        try {
            commImage = ImageIO.read(getClass().getResourceAsStream("/com/codeminders/controltower/images/gaugeback_bottom2.png"));
        } catch (IOException ex) {
            Logger.getLogger(ControlTower.class.getName()).error("{0}", ex);
        }
    }

    public void setDrone(ARDrone drone) {
        this.drone.set(drone);
        drone.addNavDataListener(this);
    }

    private void updateGauges(final NavData data) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                horizon1.setPitch(data.getPitch() * -1);
                horizon1.setRoll(data.getRoll());
                altimeter1.setValue(data.getAltitude() * 1000);
                airCompass1.setValue(data.getYaw());
                batteryGraph.setValue(data.getBattery());
                if (data.isBatteryTooLow()) {
                    batteryGraph.setThreshold(0);
                    batteryGraph.setLedBlinking(true);
                    indicator1.setOn(true);
                } else {
                    batteryGraph.setThreshold(110);
                    batteryGraph.setLedBlinking(false);
                    indicator1.setOn(false);

                }
                if (data.isUltrasonicSensorDeaf()) {
                    led1.setLedBlinking(true);
                } else {
                    led1.setLedBlinking(false);
                }
                if (data.isEmergency()) {
                    indicator2.setOn(true);
                } else {
                    indicator2.setOn(false);
                }
                if (data.isMotorsDown()) {
                    indicator3.setOn(true);
                } else {
                    indicator3.setOn(false);
                }
                if (data.isGyrometersDown()) {
                    indicator4.setOn(true);
                } else {
                    indicator4.setOn(false);
                }
            }
        });
    }

    @Override
    public void navDataReceived(NavData nd) {
        updateGauges(nd);
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(commImage, 0, 0, getWidth(), getHeight(), null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        indicator1 = new eu.hansolo.steelseries.extras.Indicator();
        indicator4 = new eu.hansolo.steelseries.extras.Indicator();
        horizon1 = new eu.hansolo.steelseries.extras.Horizon();
        airCompass1 = new eu.hansolo.steelseries.extras.AirCompass();
        altimeter1 = new eu.hansolo.steelseries.extras.Altimeter();
        led1 = new eu.hansolo.steelseries.extras.Led();
        jPanel2 = new javax.swing.JPanel();
        indicator2 = new eu.hansolo.steelseries.extras.Indicator();
        indicator3 = new eu.hansolo.steelseries.extras.Indicator();
        batteryGraph = new eu.hansolo.steelseries.gauges.Radial1Vertical();

        setOpaque(false);
        setLayout(new java.awt.GridBagLayout());

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        indicator1.setToolTipText("battery low");
        indicator1.setFrame3dEffectVisible(true);
        indicator1.setSymbolType(eu.hansolo.steelseries.tools.SymbolType.BATTERY);

        javax.swing.GroupLayout indicator1Layout = new javax.swing.GroupLayout(indicator1);
        indicator1.setLayout(indicator1Layout);
        indicator1Layout.setHorizontalGroup(
            indicator1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        indicator1Layout.setVerticalGroup(
            indicator1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        jPanel1.add(indicator1, new java.awt.GridBagConstraints());

        indicator4.setToolTipText("gyrometers down");
        indicator4.setFrame3dEffectVisible(true);
        indicator4.setSymbolType(eu.hansolo.steelseries.tools.SymbolType.BREAK);

        javax.swing.GroupLayout indicator4Layout = new javax.swing.GroupLayout(indicator4);
        indicator4.setLayout(indicator4Layout);
        indicator4Layout.setHorizontalGroup(
            indicator4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        indicator4Layout.setVerticalGroup(
            indicator4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(indicator4, gridBagConstraints);

        add(jPanel1, new java.awt.GridBagConstraints());

        horizon1.setToolTipText("artificial horizon");
        horizon1.setFrame3dEffectVisible(true);
        horizon1.setMinimumSize(new java.awt.Dimension(60, 60));

        javax.swing.GroupLayout horizon1Layout = new javax.swing.GroupLayout(horizon1);
        horizon1.setLayout(horizon1Layout);
        horizon1Layout.setHorizontalGroup(
            horizon1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );
        horizon1Layout.setVerticalGroup(
            horizon1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -20;
        gridBagConstraints.ipady = -20;
        add(horizon1, gridBagConstraints);

        airCompass1.setToolTipText("plane compass");
        airCompass1.setFrame3dEffectVisible(true);
        airCompass1.setMinimumSize(new java.awt.Dimension(60, 60));

        javax.swing.GroupLayout airCompass1Layout = new javax.swing.GroupLayout(airCompass1);
        airCompass1.setLayout(airCompass1Layout);
        airCompass1Layout.setHorizontalGroup(
            airCompass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );
        airCompass1Layout.setVerticalGroup(
            airCompass1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -20;
        gridBagConstraints.ipady = -20;
        add(airCompass1, gridBagConstraints);

        altimeter1.setToolTipText("altimeter");
        altimeter1.setFrame3dEffectVisible(true);
        altimeter1.setMinimumSize(new java.awt.Dimension(60, 60));

        javax.swing.GroupLayout led1Layout = new javax.swing.GroupLayout(led1);
        led1.setLayout(led1Layout);
        led1Layout.setHorizontalGroup(
            led1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 16, Short.MAX_VALUE)
        );
        led1Layout.setVerticalGroup(
            led1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 16, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout altimeter1Layout = new javax.swing.GroupLayout(altimeter1);
        altimeter1.setLayout(altimeter1Layout);
        altimeter1Layout.setHorizontalGroup(
            altimeter1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(altimeter1Layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(led1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(62, Short.MAX_VALUE))
        );
        altimeter1Layout.setVerticalGroup(
            altimeter1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, altimeter1Layout.createSequentialGroup()
                .addContainerGap(89, Short.MAX_VALUE)
                .addComponent(led1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -60;
        gridBagConstraints.ipady = -60;
        add(altimeter1, gridBagConstraints);

        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.GridBagLayout());

        indicator2.setToolTipText("emergency (click to reset)");
        indicator2.setFrame3dEffectVisible(true);
        indicator2.setSymbolType(eu.hansolo.steelseries.tools.SymbolType.ATTENTION);
        indicator2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                resetDrone(evt);
            }
        });

        javax.swing.GroupLayout indicator2Layout = new javax.swing.GroupLayout(indicator2);
        indicator2.setLayout(indicator2Layout);
        indicator2Layout.setHorizontalGroup(
            indicator2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        indicator2Layout.setVerticalGroup(
            indicator2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        jPanel2.add(indicator2, new java.awt.GridBagConstraints());

        indicator3.setToolTipText("engine problem");
        indicator3.setFrame3dEffectVisible(true);
        indicator3.setSymbolType(eu.hansolo.steelseries.tools.SymbolType.ENGINE);

        javax.swing.GroupLayout indicator3Layout = new javax.swing.GroupLayout(indicator3);
        indicator3.setLayout(indicator3Layout);
        indicator3Layout.setHorizontalGroup(
            indicator3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );
        indicator3Layout.setVerticalGroup(
            indicator3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel2.add(indicator3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        add(jPanel2, gridBagConstraints);

        batteryGraph.setToolTipText("battery status");
        batteryGraph.setFrame3dEffectVisible(true);
        batteryGraph.setThreshold(110.0);
        batteryGraph.setTitle("Battery");
        batteryGraph.setTrackStartColor(new java.awt.Color(255, 0, 0));
        batteryGraph.setTrackStopColor(new java.awt.Color(0, 255, 0));
        batteryGraph.setTrackVisible(true);
        batteryGraph.setUnitString("%");

        javax.swing.GroupLayout batteryGraphLayout = new javax.swing.GroupLayout(batteryGraph);
        batteryGraph.setLayout(batteryGraphLayout);
        batteryGraphLayout.setHorizontalGroup(
            batteryGraphLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );
        batteryGraphLayout.setVerticalGroup(
            batteryGraphLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = -60;
        gridBagConstraints.ipady = -60;
        add(batteryGraph, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void resetDrone(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resetDrone
        ARDrone drone=this.drone.get();
        if(drone!=null){
            try {
                drone.clearEmergencySignal();
            } catch (IOException ex) {
                Logger.getLogger(BottomGaugePanel.class.getName()).error("", ex);
            }
        }
    }//GEN-LAST:event_resetDrone

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private eu.hansolo.steelseries.extras.AirCompass airCompass1;
    private eu.hansolo.steelseries.extras.Altimeter altimeter1;
    private eu.hansolo.steelseries.gauges.Radial1Vertical batteryGraph;
    private eu.hansolo.steelseries.extras.Horizon horizon1;
    private eu.hansolo.steelseries.extras.Indicator indicator1;
    private eu.hansolo.steelseries.extras.Indicator indicator2;
    private eu.hansolo.steelseries.extras.Indicator indicator3;
    private eu.hansolo.steelseries.extras.Indicator indicator4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private eu.hansolo.steelseries.extras.Led led1;
    // End of variables declaration//GEN-END:variables
}
